package com.dream.inspect.modules.business.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.toolkit.StringUtils;
import com.dream.inspect.common.exception.RRException;
import com.dream.inspect.common.utils.DateFormatUtil;
import com.dream.inspect.common.utils.DateUtils;
import com.dream.inspect.common.utils.PageUtils;
import com.dream.inspect.common.utils.Query;
import com.dream.inspect.common.validator.ValidatorUtils;
import com.dream.inspect.common.validator.group.AddGroup;
import com.dream.inspect.common.validator.group.UpdateGroup;
import com.dream.inspect.modules.business.dao.SuperviseQuestionModifyDao;
import com.dream.inspect.modules.business.entity.SuperviseQuestionModifyEntity;
import com.dream.inspect.modules.business.service.SuperviseQuestionModifyService;


@Service("superviseQuestionModifyService")
public class SuperviseQuestionModifyServiceImpl extends ServiceImpl<SuperviseQuestionModifyDao, SuperviseQuestionModifyEntity> implements SuperviseQuestionModifyService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<SuperviseQuestionModifyEntity> page = this.selectPage(
                new Query<SuperviseQuestionModifyEntity>(params).getPage(),
                new EntityWrapper<SuperviseQuestionModifyEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 查询整改记录(不分页)
     * 
     * @param params 查询条件
     * @return
     */
	@Override
	public List<SuperviseQuestionModifyEntity> findSuperviseQuestionModifyList(Map<String, String> params) {
		List<SuperviseQuestionModifyEntity> sqrList = new ArrayList<SuperviseQuestionModifyEntity>();
		//组装条件
		EntityWrapper<SuperviseQuestionModifyEntity> wrapper = addCondition(params);
		sqrList = baseMapper.findSuperviseQuestionModifyList(wrapper);
		return sqrList;
	}
	

    @Override
    public List<SuperviseQuestionModifyEntity> findSuperviseQuestionModifyListByPage(Map<String, String> params) {
        int currPage = Integer.valueOf(params.get("currPage"));
        int pageSize  = Integer.valueOf(params.get("pageSize"));
        currPage = (currPage - 1) * pageSize;
        List<SuperviseQuestionModifyEntity> sqrList = new ArrayList<SuperviseQuestionModifyEntity>();
        //组装条件
        EntityWrapper<SuperviseQuestionModifyEntity> wrapper = addCondition(params);
        sqrList = baseMapper.findSuperviseQuestionModifyListByPage(wrapper, currPage, pageSize);
        return sqrList;
    }

	@Override
	public int findSuperviseQuestionModifyListCount(Map<String, String> params) {
		//组装条件
		EntityWrapper<SuperviseQuestionModifyEntity> wrapper = addCondition(params);
		int countNum = baseMapper.findSuperviseQuestionModifyListCount(wrapper);
		return countNum;
	}
	

    @Override
    public List<SuperviseQuestionModifyEntity> findSuperviseQuestionModifyHisDetail(Map<String, String> params) {
        String userDeptName = params.get("userDeptName");
        int currPage = Integer.valueOf(params.get("currPage"));
        int pageSize  = Integer.valueOf(params.get("pageSize"));
        currPage = (currPage - 1) * pageSize;
        List<SuperviseQuestionModifyEntity> sqrList = new ArrayList<SuperviseQuestionModifyEntity>();
        //组装条件
        EntityWrapper<SuperviseQuestionModifyEntity> wrapper = addCondition(params);
        sqrList = baseMapper.findSuperviseQuestionModifyHisByPage(wrapper, currPage, pageSize, userDeptName);
        return sqrList;
    }
	
    @Override
    public List<SuperviseQuestionModifyEntity> findSuperviseQuestionModifyHisByPage(Map<String, String> params) {
        String userDeptName = params.get("userDeptName");
        String currPageTemp = params.get("currPage");
        int currPage = 1;
        if(StringUtils.isNotEmpty(currPageTemp)) {
        	currPage = Integer.valueOf(currPageTemp);
        }
        
        String pageSizeTemp = params.get("pageSize");
        int pageSize = 10;
        if(StringUtils.isNotEmpty(pageSizeTemp)) {
        	pageSize  = Integer.valueOf(pageSizeTemp);
        }
        
        currPage = (currPage - 1) * pageSize;
        List<SuperviseQuestionModifyEntity> sqrList = new ArrayList<SuperviseQuestionModifyEntity>();
        //组装条件
        EntityWrapper<SuperviseQuestionModifyEntity> wrapper = addCondition(params);
        sqrList = baseMapper.findSuperviseQuestionModifyHisByPage(wrapper, currPage, pageSize, userDeptName);
        return sqrList;
    }

    @Override
    public int findSuperviseQuestionModifyHisCount(Map<String, String> params) {
        String userDeptName = params.get("userDeptName");
        //组装条件
        EntityWrapper<SuperviseQuestionModifyEntity> wrapper = addCondition(params);
        int countNum = baseMapper.findSuperviseQuestionModifyHisCount(wrapper, userDeptName);
        return countNum;
    }
    
    @Override
    public SuperviseQuestionModifyEntity checkFindSuperviseQuestionModify(Map<String, String> params) {
        long modifyId = Long.valueOf(params.get("modifyId"));
        SuperviseQuestionModifyEntity sqmEntity = new SuperviseQuestionModifyEntity();
        sqmEntity = this.selectById(modifyId);
        return sqmEntity;
    }
    

    @Override
    public boolean updateCheckSuperviseQuestionModify(Map<String, String> params) {
        
        long modifyId = Long.valueOf(params.get("modifyId"));
        String dcQuestionId = params.get("dcQuestionId");         //督察问题ID
        String dcRecordId = params.get("dcRecordId");          //督察记录ID
        String fkshr = params.get("fkshr");                    //反馈审核人
        String fkshsj = params.get("fkshsj");                   //反馈审核时间
        String checkStatus = params.get("checkStatus");              //审核状态(0：待审核，1：审核通过，2：审核未通过)
        String checkResult = params.get("checkResult");              //审核结果(0：考核扣分，1：不予处理，2：其他)
        String checkResultDetail = params.get("checkResultDetail");     //审核结果说明
        //审核单位
        String kffz = params.get("kffz");     //审核结果说明
        String userId = params.get("userId");
        
        SuperviseQuestionModifyEntity sqmEntity = new SuperviseQuestionModifyEntity();
        if (StringUtils.isNotEmpty(String.valueOf(modifyId))) {
            sqmEntity.setId(modifyId);
        }
        if (StringUtils.isNotEmpty(dcQuestionId)) {
            sqmEntity.setDcQuestionId(Long.valueOf(dcQuestionId));
        }
        if (StringUtils.isNotEmpty(dcRecordId)) {
            sqmEntity.setDcRecordId(Long.valueOf(dcRecordId));
        }
        if (StringUtils.isNotEmpty(fkshr)) {
            sqmEntity.setFkshr(fkshr);
        }
        if (StringUtils.isNotEmpty(fkshsj)) {
            sqmEntity.setFkshsj(DateUtils.stringToDate(fkshsj, DateUtils.DATE_TIME_PATTERN));
        }
        if (StringUtils.isNotEmpty(checkStatus)) {
            sqmEntity.setCheckStatus(checkStatus);
        }
        if (StringUtils.isNotEmpty(checkResult)) {
            sqmEntity.setCheckResult(checkResult);
            if ("0".equals(checkResult)) {
                sqmEntity.setKffz(Integer.valueOf(kffz));
            }
            if ("2".equals(checkResult)) {
                sqmEntity.setCheckResultDetail(checkResultDetail);
            }
        }
        if (StringUtils.isNotEmpty(userId)) {
            sqmEntity.setUpdateUser(Long.valueOf(userId));
            sqmEntity.setUpdateTime(new Date());
        }
        
        ValidatorUtils.validateEntity(sqmEntity, UpdateGroup.class);
        return this.updateById(sqmEntity);
    }
    
    @Override
    public long insertFeedBackSuperviseQuestionModify(Map<String, String> params) {
        
        Date now = new Date();
        String dcQuestionId = params.get("dcQuestionId"); //督察问题id  
        String dcRecordId = params.get("dcRecordId");  //督察记录id 
        String modifyTime = params.get("modifyTime");  //整改时间   
        String toDcry = params.get("toDcry");   //被督察人员
        String fkry = params.get("fkry");   //反馈人员  
        String fksj = params.get("fksj");   //反馈时间  
        String fkdw = params.get("fkdw");    //反馈单位 
        String zgfknr = params.get("zgfknr"); //整改反馈内容
        String userId = params.get("userId");
        SuperviseQuestionModifyEntity sqmEntity = new SuperviseQuestionModifyEntity();
        
        if (StringUtils.isNotEmpty(dcQuestionId)) {
            sqmEntity.setDcQuestionId(Long.valueOf(dcQuestionId));
        }
        if (StringUtils.isNotEmpty(dcRecordId)) {
            sqmEntity.setDcRecordId(Long.valueOf(dcRecordId));
        }
        if (StringUtils.isNotEmpty(modifyTime)) {
            sqmEntity.setModifyTime(DateUtils.stringToDate(modifyTime, DateUtils.DATE_TIME_PATTERN));
        }
        if (StringUtils.isNotEmpty(toDcry)) {
            sqmEntity.setToDcry(toDcry);
        }
        if (StringUtils.isNotEmpty(fkry)) {
            sqmEntity.setFkry(fkry);
        }
        if (StringUtils.isNotEmpty(fksj)) {
            sqmEntity.setFksj(DateUtils.stringToDate(fksj, DateUtils.DATE_TIME_PATTERN));
        }
        if (StringUtils.isNotEmpty(fkdw)) {
            sqmEntity.setFkdw(fkdw);
        }
        if (StringUtils.isNotEmpty(zgfknr)) {
            sqmEntity.setZgfknr(zgfknr);
        }
        if (StringUtils.isNotEmpty(userId)) {
            sqmEntity.setCreateUser(Long.valueOf(userId)); 
            sqmEntity.setCreateTime(now);
            sqmEntity.setUpdateUser(Long.valueOf(userId));
            sqmEntity.setUpdateTime(now);
        }
        
        try {
            ValidatorUtils.validateEntity(sqmEntity, AddGroup.class);
        } catch (RRException rre) {
            throw rre;
        }
        //save
        this.insert(sqmEntity);
        
        return sqmEntity.getId();
    }
    
	@Override
	public int selectThisMonthCount(Map<String, String> params) {
		//查询条件
        EntityWrapper<SuperviseQuestionModifyEntity> wrapper = addCondition(params);

        int countNum = baseMapper.selectThisMonthCount(wrapper);
        return countNum;
	}
	
	@Override
	public SuperviseQuestionModifyEntity findSuperviseQuestionMForSearchPage(Map<String, String> params) {
		String id = params.get("id");
        return baseMapper.findSuperviseQuestionMForSearchPage(id);
	}

	@Override
	public int findSuperviseQuestionMForSearchPageCount(Map<String, String> params) {
		//查询条件
        EntityWrapper<SuperviseQuestionModifyEntity> wrapper = addCondition(params);

        int countNum = baseMapper.findSuperviseQuestionMForSearchPageCount(wrapper);
        return countNum;
	}
	
	@Override
	public PageUtils selectSuperviseQuestionModifyListPage(Map<String, String> params) {
		String currP = params.get("currPage");
		int currPage = 1;
		if (StringUtils.isNotEmpty(currP)) {
			currPage = Integer.valueOf(currP);
		}
		String pageS = params.get("pageSize");
		int pageSize  = 10;
		if (StringUtils.isNotEmpty(pageS)) {
			pageSize  = Integer.valueOf(pageS);
		}
		
		Page<SuperviseQuestionModifyEntity> page = new Page<>(currPage, pageSize);
		SuperviseQuestionModifyEntity sqmEntity = new SuperviseQuestionModifyEntity();
		String deptIdTemp = params.get("deptId");
		if (StringUtils.isNotEmpty(deptIdTemp)) {
			Long deptId = Long.valueOf(deptIdTemp);
			sqmEntity.setDeptId(deptId);
		}
        if (StringUtils.isNotEmpty(params.get("v2"))) {
            sqmEntity.setV2(params.get("v2"));
        }
		String findKey = params.get("findKey");
		if (StringUtils.isNotEmpty(findKey)) {
			sqmEntity.setV1(findKey);
		}
		page.setRecords(this.baseMapper.selectSuperviseQuestionModifyListPage(page, sqmEntity));
        return new PageUtils(page);
	}
	
	/**
	 * 拼接查询条件
	 * 
	 * @param params
	 * @return
	 */
	private EntityWrapper<SuperviseQuestionModifyEntity> addCondition(Map<String, String> params) {
		String id = params.get("dcRecordId");
		String projectId = params.get("projectId");
		String questionId = params.get("questionId");
		
		String sortByTime = params.get("sortByTime");
		String checkStatus = params.get("checkStatus");
		String dcfs = params.get("dcfs");
		//String userDeptName = params.get("userDeptName");
		String thisMonthFlg = params.get("thisMonthFlg");//本月标志
		String searchText = params.get("searchText");
		String questionModifyId = params.get("questionModifyId");
		
		EntityWrapper<SuperviseQuestionModifyEntity> wrapper = new EntityWrapper<SuperviseQuestionModifyEntity>();
		wrapper.setEntity(new SuperviseQuestionModifyEntity());
		//问题整改ID
		if (StringUtils.isNotEmpty(questionModifyId)) {
			wrapper.eq("sqm.ID", questionModifyId);
		}
		//督察记录ID
		if (StringUtils.isNotEmpty(id)) {
			wrapper.eq("sqm.DC_RECORD_ID", id);
		}
		//专项ID
		if (StringUtils.isNotEmpty(projectId)) {
			wrapper.eq("PROJECT_ID", projectId);
		}
		//问题ID
        if (StringUtils.isNotEmpty(questionId)) {
            wrapper.eq("sqm.DC_QUESTION_ID", questionId);
        }
        //审核状态(0：待审核，1：审核通过，2：审核未通过)
        if (StringUtils.isNotEmpty(checkStatus)) {
            wrapper.eq("sqm.CHECK_STATUS", checkStatus);
        }
        //督察方式(1：实时拍录，2：立项督察，3：分级督察)
        if (StringUtils.isNotEmpty(dcfs)) {
            wrapper.eq("sr.DCFS", dcfs);
        }
        //用户部门名称
        /*if (StringUtils.isNotEmpty(userDeptName)) {
            wrapper.eq("sq.DCDW", userDeptName);
        }*/
        if (StringUtils.isNotEmpty(thisMonthFlg)) {
			if ("1".equals(thisMonthFlg)) {
				String dt = DateFormatUtil.getFirstDayOfMonthForString();
				wrapper.and(" Date(sqm.CREATE_TIME) >= '" + dt + "'");
			}
		}
        
        //全文检索条件
		if (StringUtils.isNotEmpty(searchText)) {
			wrapper.andNew();
	  		wrapper.like("ss.QUESTION_DESCRIPTION", searchText);
	  		wrapper.or("sq.DCDW like {0}", "%" + searchText + "%");
		}
        //时间排序（1：升序，2：降序）
        if (StringUtils.isNotEmpty(sortByTime)) {
            if ("1".equals(sortByTime)) {
                wrapper.orderBy("sq.DCSJ", true);
            } else {
                wrapper.orderBy("sq.DCSJ", false);
            }
        }
    
    	return wrapper;
	}

}
